#!/usr/bin/python
import os
import sys
import re

if len(sys.argv)<2: raise Exception('Require one argument.')
if os.path.exists(sys.argv[1]): raise Exception('%s exists.'%sys.argv[1])
out=open(sys.argv[1],'w')

# downloaded patch are stored into /source/

os.chdir(os.path.dirname(os.path.abspath(sys.argv[0]))) # chdir to the directory where `unpack' stays
f=open('WhereAreThey','r')
for line in f:
	line = line.strip()
	if line[0]=='#': continue # skip comments
	if line.find('http://')!=0: raise Exception('File `WhereAreThey` format error.')
	filename=re.search('[^/]+$',line)
	if filename==None: raise Exception('File `WhereAreThey` format error.')
	filename=filename.group(0)
	path='/source/'+filename
	out.write(path+'\n')
	if os.path.exists(path): continue
	cmd='wget -c --tries=20 %s'%line # continue getting, 20 tries, turn off verbose without being completely quiet
	if os.system(cmd): raise Exception('Cannot download %s'%filename, cmd)
	if os.system('mv %s /source/'%filename): raise Exception('Cannot move %s into /source/'%filename)
	
sys.exit(0)
